package jmine.tec.utils.cripto.key.impl;

import java.security.KeyStoreException;
import java.security.PublicKey;

import org.apache.commons.logging.LogFactory;

import jmine.tec.utils.cripto.key.PublicKeyLocator;
import jmine.tec.utils.cripto.key.store.KeyStoreReader;

/**
 * {@link PublicKeyLocator} que procura a chave publica a partir de um {@link KeyStoreReader}
 * 
 * @author takeshi
 */
public class KeyStorePublicKeyLocator implements PublicKeyLocator {

    private KeyStoreReader keyStoreReader;

    /**
     * 
     */
    public KeyStorePublicKeyLocator() {
    }

    /**
     * @param keyStoreReader {@link KeyStoreReader}
     */
    public KeyStorePublicKeyLocator(KeyStoreReader keyStoreReader) {
        this.keyStoreReader = keyStoreReader;
    }

    /**
     * @return the keyStoreReader
     */
    public KeyStoreReader getKeyStoreReader() {
        return keyStoreReader;
    }

    /**
     * @param keyStoreReader the keyStoreReader to set
     */
    public void setKeyStoreReader(KeyStoreReader keyStoreReader) {
        this.keyStoreReader = keyStoreReader;
    }

    /**
     * {@inheritDoc}
     */
    public PublicKey getPublicKey(String keyName) {
        try {
            return keyStoreReader.getPublicKey(keyName);
        } catch (KeyStoreException e) {
            LogFactory.getLog(getClass()).warn("Exception reading keyStore", e);
            return null;
        }
    }

}
